package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.ht.medicaltreatment.bean.BoxSubmitBean
import com.ht.medicaltreatment.bean.TransportRfidSampleBox
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class ScanViewModel: BaseViewModel() {
    fun scanData(uid:String,orderId:String,inOrOut:String,onResult:(TransportRfidSampleBox)->Unit,onError:(String)->Unit){
        viewModelScope.launch {

            val bean = mApi.scanData(uid,orderId,"积水潭医院",inOrOut)
            when(bean){
                is ApiResult.Success -> {
                    val list = bean.data.data?.transportRfidSampleBoxList
                    if(list != null && list.size > 0){
                        onResult(list[0])
                    }
                }
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    /**
     * 替换芯片
     */
    fun replaceUID(id : String,uid : String,onResult: (String) -> Unit,onError: (String) -> Unit) {
        viewModelScope.launch {
            val bean = mApi.replaceUID(id,uid)
            when(bean){
                is ApiResult.Success -> onResult("")
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun submit(boxUID: String, onResult: (BoxSubmitBean) -> Unit, onError: (String) -> Unit) {
        viewModelScope.launch {
            val bean = mApi.submit(boxUID)
            when(bean){
                is ApiResult.Success ->onResult(bean.data.data!!)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    /**
     * 查询uid对应的名字
     */
    fun searchName(uid:MutableList<String>, onResult:(MutableList<String>)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            when(val bean = mApi.searchNameScan(uid)){
                is ApiResult.Success -> {
                    val sampleInfoList = bean.data.data?.sampleInfoList
                    if(sampleInfoList != null && sampleInfoList.size > 0){
                        val list = arrayListOf<String>()
                        sampleInfoList.forEach {
                            list += it.name
                        }
                        onResult(list)
                    }else{
                        onError("无数据")
                    }
                }
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }
}
